<template>

  <lam-select :list="options" :value="actualValue" :useEmptyVal="useEmptyVal" :placeholder="placeholder" :disabled="disabled"
    :v-model="value" :type="type" :clearable="clearable" :collapseTags="collapseTags" :useSpliter="useSpliter" :spliter="spliter"
    :border="border" :size="size" @change="onChange" @visible-change="onVisibleChange" @remove-tag="onRemoveTag" @clear="onClear"
     @blur="onBlur" @focus="onFocus">
    <template slot-scope="{item}" slot="option">
      <slot name="option" :item="item"></slot>
    </template>
  </lam-select>

</template>

<script>

  export default {
    name: "LamDictSelect",
    model:{
      prop: 'value',
      event: 'change'
    },
    props: {
      dictCode: String,
      // dictType 支持两个值：tableDict 和 dict
      dictType: {
        type: String,
        default: "dict"
      },
      useEmptyVal: {
        type: Boolean,
        default: true
      },
      placeholder: String,
      disabled: {
        type: Boolean,
        default: false
      },
      collapseTags: {
        type: Boolean,
        default: true
      },
      filterable: {
        type: Boolean,
        default: false
      },
      value: [String, Number, Array],
      type: String,
      clearable: {
        type: Boolean,
        default: true
      },
      useSpliter: {
        type: Boolean,
        default: true
      },
      spliter: {
        type: String,
        required: false,
        default: ','
      },
      border: {
        type: Boolean,
        default: false
      },
      size:{
        type: String
      }
    },
    data() {
      return {
        options: []
      }
    },
    watch:{
      dictCode:{
        immediate:true,
        handler() {
          this.initData()
        },
      }
    },
    created() {
      //获取字典数据
      //this.initDictData();
    },
    computed: {
      actualValue(){
        return (this.value===0||!!this.value)?this.value : undefined;
      },
    },
    methods: {
      initData() {
        this.$getDictItems(this.dictCode, this.dictType).then(data => {
          this.options = data;
        });
      },
      onChange(val) {
        this.$emit('change', val);
      },
      onVisibleChange(val){
        this.$emit('visibleChange', val);
      },
      onRemoveTag(val){
        this.$emit('removeTag', val);
      },
      onClear(val){
        this.$emit('clear', val);
      },
      onBlur(val){
        this.$emit('blur', val);
      },
      onFocus(val){
        this.$emit('focus', val);
      },
      setOptions(options){
        this.options = options
      },
      getOptions(){
        return this.options
      }
    }
  }
</script>

<style scoped>
</style>
